package com.ksd.modules.course.controller;

import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.web.annotation.RepeatSubmit;
import com.ksd.modules.course.po.AdminCourseCategorySavePO;
import com.ksd.modules.course.po.AdminCourseCategorySearchPO;
import com.ksd.modules.course.service.IAdminCourseCategoryService;
import com.ksd.modules.course.vo.AdminCourseCategoryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AdminCourseCategoryController extends BaseController {

    @Autowired
    private IAdminCourseCategoryService adminCourseCategoryService;

    /**
     * 【课程】- 查询分类列表
     * @param adminCourseCategorySearchPO
     * @return
     */
    @PostMapping("/course/category/treeall")
    public R<List<AdminCourseCategoryVo>> findAdminCourseCategoryAll(@RequestBody AdminCourseCategorySearchPO adminCourseCategorySearchPO){
        return R.data(adminCourseCategoryService.findAdminCourseCategory(adminCourseCategorySearchPO));
    }

    /**
     * 【课程】- 查询分类列表--只查询发布的分类
     * @param adminCourseCategorySearchPO
     * @return
     */
    @PostMapping("/course/category/tree")
    public R<List<AdminCourseCategoryVo>> findAdminCourseCategory(@RequestBody AdminCourseCategorySearchPO adminCourseCategorySearchPO){
        adminCourseCategorySearchPO.setStatus(StatusEnums.STATUS.getCode());
        return R.data(adminCourseCategoryService.findAdminCourseCategory(adminCourseCategorySearchPO));
    }

    /**
     * 【课程】- 查询根分类
     * @return
     */
    @PostMapping("/course/category/root")
    public R<List<AdminCourseCategoryVo>> findAdminCourseCategoryRoot(){
        return R.data(adminCourseCategoryService.findAdminCourseCategoryRoot());
    }

    /**
     * 【课程】- 根据id删除分类
     * @return
     */
    @PostMapping("/course/category/del/{id}")
    @ApiLog("【课程】- 根据id删除分类")
    public R<Boolean> deleteAdminCourseCategoryById(@PathVariable("id")Long id){
        return R.data(adminCourseCategoryService.deleteAdminCourseCategoryById(id));
    }

    /**
     * 【课程】- 根据id获取分类信息
     * @return
     */
    @PostMapping("/course/category/get/{id}")
    public R<AdminCourseCategoryVo> getAdminCourseCategoryById(@PathVariable("id")Long id){
        return R.data(adminCourseCategoryService.getAdminCourseCategoryById(id));
    }

    /**
     * 【课程】- 保存和修改分类信息
     * @return
     */
    @PostMapping("/course/category/saveupdate")
    @ApiLog("【课程】- 保存和修改分类信息")
    public R<Boolean> saveUpdateAdminCourseCategory(@RequestBody AdminCourseCategorySavePO adminCourseCategorySavePO){
        return R.data(adminCourseCategoryService.saveUpdateAdminCourseCategory(adminCourseCategorySavePO));
    }

    /**
     * 【课程】- 修改状态
     *
     * @return
     */
    @PostMapping("/course/category/status/{id}/{field}/{value}")
    @ApiLog("【课程】- 分类状态修改")
    @RepeatSubmit
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @PathVariable("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(adminCourseCategoryService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(adminCourseCategoryService.updateStatusStr(id, field, String.valueOf(value)));
        }
    }

    /**
     * 【课程分类】-  复制数据
     * @param opid
     * @return
     */
    @PostMapping("/course/category/copy/{opid}")
    @ApiLog("【课程分类】- 复制数据")
    @RepeatSubmit
    public R<Boolean> copyData(@PathVariable("opid")Long opid){
        return R.data(adminCourseCategoryService.copyData(opid));
    }

}
